package org.pluslibs.framework.swingplus.jpanel;

import java.awt.Insets;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;

import org.apache.commons.lang.StringUtils;

public class JPanelButtons<T extends Enum<T>> extends JPanelFields<T> {
    private static final long serialVersionUID = 6448243545522998274L;

    private static final int SET_SELECTED_DEFULT = -1;
    private int defaultSelected_x = SET_SELECTED_DEFULT;
    private int defaultSelected_y = SET_SELECTED_DEFULT;

    private List<List<AbstractButton>> buttons;

    public JPanelButtons(AbstractButton[]... buttons) {
        this(false, buttons);
    }

    public JPanelButtons(Insets insets, AbstractButton[]... buttons) {
        super(false);
        this.insets = insets;
        initalize(buttons);
    }

    public JPanelButtons(int defaultSelected_x, int defaultSelected_y, AbstractButton[]... buttons) {
        this(null, false, defaultSelected_x, defaultSelected_y, buttons);
    }

    public JPanelButtons(String title, AbstractButton[]... buttons) {
        this(title, false, buttons);
    }

    public JPanelButtons(boolean border, AbstractButton[]... buttons) {
        super(border);
        initalize(buttons);
    }

    public JPanelButtons(String title, boolean border, AbstractButton[]... buttons) {
        super(title, border);
        initalize(buttons);
    }

    public JPanelButtons(String title, boolean border, int defaultSelected_x, int defaultSelected_y, AbstractButton[]... buttons) {
        super(title, border);
        this.defaultSelected_x = defaultSelected_x;
        this.defaultSelected_y = defaultSelected_y;
        initalize(buttons);
    }

    private void initalize(AbstractButton[]... buttonsArray) {
        setLayoutCentered();
        buttons = new ArrayList<List<AbstractButton>>();
        ButtonGroup group = new ButtonGroup();
        for (AbstractButton[] buttonsRow : buttonsArray) {
            List<AbstractButton> temp = new ArrayList<AbstractButton>();
            for (AbstractButton button : buttonsRow) {
                button.setActionCommand(button.getName());
                group.add(button);
                temp.add(button);
            }
            addRow(temp.toArray(new AbstractButton[temp.size()]));
            buttons.add(temp);
        }
        setDefaultSelected();
    }

    private void setDefaultSelected() {

        if ((defaultSelected_x > SET_SELECTED_DEFULT) && (defaultSelected_y > SET_SELECTED_DEFULT) && (defaultSelected_x <= buttons.size())
                && (defaultSelected_y <= buttons.get(defaultSelected_x).size())) {
            this.buttons.get(defaultSelected_x).get(defaultSelected_y).setSelected(true);
        }
    }

    public void addActionListener(ActionListener actionListener) {
        for (List<AbstractButton> row : buttons) {
            for (AbstractButton button : row) {
                button.addActionListener(actionListener);
            }
        }
    }

    public void setSelected(String actionCommand) {
        for (List<AbstractButton> rows : buttons) {
            for (AbstractButton button : rows) {
                if (StringUtils.equals(actionCommand, button.getActionCommand())) {
                    button.setSelected(true);
                }
                button.setSelected(false);
            }
        }
    }

    public String getSelected() {
        for (List<AbstractButton> rows : buttons) {
            for (AbstractButton button : rows) {
                if (button.isSelected()) {
                    return button.getActionCommand();
                }
            }
        }
        return null;
    }

    @Override
    public void setEnabled(boolean enabled) {
        for (List<AbstractButton> row : buttons) {
            for (AbstractButton abstractButton : row) {
                abstractButton.setEnabled(enabled);
            }
        }
        super.setEnabled(enabled);
    }
}
